<template>
  <div class="login-container">
    <div class="account-header">
      <ul>
        <li
          class="hand"
          @click="toLogin"
        >登录</li>
        <li class="hand active"><a>找回密码</a></li>
      </ul>
    </div>
    <div class="account-body">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >

        <el-form-item
          class="no-nowrap"
          prop="username"
        >
          <span class="svg-container">
            <svg-icon icon-class="phone" />
          </span>
          <el-input
            v-model="loginForm.username"
            :placeholder="$t('login.username')"
            name="username"
            type="text"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item
          class="no-nowrap"
          prop="password"
        >
          <el-input
            class="getcode-input"
            :type="passwordType"
            v-model="loginForm.password"
            :placeholder="$t('login.password')"
            name="password"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="getcode-container">
            获取验证码
          </span>
        </el-form-item>
        <el-form-item
          class="no-nowrap"
          prop="code"
        >
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :type="passwordType"
            v-model="loginForm.password"
            :placeholder="$t('login.password')"
            name="password"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span
            class="show-pwd"
            @click="getCode"
          >
            <svg-icon icon-class="eye" />
          </span>
        </el-form-item>
        <el-form-item
          class="no-nowrap func-form-item"
          prop="password"
        >
          <div class="flex-row flex-justify-e">
            <el-checkbox v-model="agree">我已同意</el-checkbox><a
              class="register-link"
            >《注册协议》</a>
          </div>
        </el-form-item>
        <button
          type="button"
          class="account-btn ivu-btn"
        >
          <span>注册</span></button>
      </el-form>

    </div>
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect'
import '@/styles/login.scss'
import mixins from '@/views/login/mixins'
export default {
  name: 'Register',
  mixins: [mixins],
  components: {
    LangSelect
  },
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '1111111',
        code: '',
        agree: false
      },
      loading: false,
      showDialog: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      // 监听router
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {
    getCode() {

    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('LoginByUsername', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({
              path: this.redirect || '/'
            })
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    afterQRScan() {
      // const hash = window.location.hash.slice(1)
      // const hashObj = getQueryObject(hash)
      // const originUrl = window.location.origin
      // history.replaceState({}, '', originUrl)
      // const codeMap = {
      //   wechat: 'code',
      //   tencent: 'code'
      // }
      // const codeName = hashObj[codeMap[this.auth_type]]
      // if (!codeName) {
      //   alert('第三方登录失败')
      // } else {
      //   this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
      //     this.$router.push({ path: '/' })
      //   })
      // }
    },
    toLogin() {
      this.$router.push({
        path: '/login'
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scope>
</style>
